<?php

class HomepageController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		return View::make("homepage.homepage");
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$input = Input::all();



		$validator = Validator::make(
			array(
				'name' => $input['std_name'],
				'department' => $input['std_department'],
				'std_file' => $input['std_file'],
				'time' => time(),
                'id' => $input['id'],
                'std_id' => $input['std_id']
				),
			array(
				'name' => 'required|max:10',
				'department' => 'required',
				'std_file' => 'required',
                'id' => "required"
				)
			);
		if ($validator->fails()){
			return Response::json(array('success' => false, "error" => $validator->errors()->toArray()));
		}else{
			$file = Input::file('std_file');


			if (Input::hasFile('std_file')) {

				//检验一下上传的文件是否有效.
			    //$clientName = $file -> getClientOriginalName();
				//客户端文件名称..
				//$tmpName = $file ->getFileName();
				//缓存在tmp文件夹中的文件名例如php8933.tmp 这种类型的.
			   	//$realPath = $file -> getRealPath();
			   	//这个表示的是缓存在tmp文件夹下的文件的绝对路径
			   	$extension = $file -> getClientOriginalExtension();
			   	//上传文件的后缀.
			   	$mimeTye = $file -> getMimeType();
			   	//文件类型
			   	$gb_username=iconv("UTF-8","gb2312",$input['std_name']);
			   	//把人名转换字符编码
			   	$newName = $gb_username."+".$input['std_id'].".".$extension;
			   	//新名字 人名 + 学号 不会重复
			   	$path = $file -> move(app_path().'/storage/uploads',$newName);
			   	//移动路径

			   	DB::table('workup')->insert(array(
			   		'up_file_name' => $newName,
			   		'up_type' => $mimeTye,
					'up_user_id' => $input['id'],
					'up_work_id' => $input['work_id'],
					'up_create_time' => time(),
				));

				return Response::json(['success' => true]);
			}
		}
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

	public function sum()
	{
		if (Request::ajax()) 
		{
			
			$Web_num = DB::table('homework')->where('department', '=', '1')->count();

			$Mobile_num = DB::table('homework')->where('department', '=', '2')->count();
			
			return  Response::json(array('Web_num' => $Web_num, 'Mobile_num' => $Mobile_num));
		}
		
	}
}
